<?php
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';

$url = MiscUtils::getParam('url', NULL);
$path = basename(dirname(__FILE__));
$sys_script = $system->script;

$cond_vals = new stdClass();
$cond_vals->c = 't.sys_id = :v1 AND t.is_visible = 1';
$cond_vals->v = array(':v1' => $system->id);
$opts = new stdClass();
$opts->select_expr = 't.id, t.parent_id, t.title as name';
$menus = DbUtils::get($myPdo, 'pz2_page', $cond_vals, 't.order', 'ASC', NULL, NULL, $opts)->d;

$mRoot = new stdClass();
$mRoot->id = 0;
$mRoot->name = 'ROOT';
$mRoot->link = '';
$mRoot->c = array();
_createMenuRoot($menus, $mRoot);

$mRoot2 = new stdClass();
$mRoot2->id = 0;
$mRoot2->name = 'ROOT';
$mRoot2->link = '';
$mRoot2->c = array();
_createMenuRoot($menus, $mRoot2);

$nav = _getNav($mRoot2, $page_id);
if (count($nav) >= 1) {
  $page_id = $nav[0]->id;
  if (count($nav) == 1) {
    $nav = array();
  }
}
?>
<html>
<head>
<title><?php echo $system->title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  outline: 0;
  margin: 0;
}
body {
  background-image: url(exts/uis/<?php echo $path; ?>/assets/imgs/bg.png);
  background-repeat: repeat;
}
.superscript {
  background-color: #653D3D;
  border-bottom: 1px solid #ddd;
  text-align: center;
}
.menubar td.menu {
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  background-color: #825050;
  min-width: 80px;
  border: 1px solid #825050;
  border-bottom: 0;
}
.menubar td.menu a {
  background-image: none !important;
  font-size: 14px;
  color: #fff;
  text-decoration: none;
  letter-spacing: 0;
  font-weight: bold;
}
.hi {
  background-color: #D9D6CD !important;
  border: 1px solid #D9D6CD;
  font-weight: bold;
}
.hi a {
  color: #825050 !important;
}
.right {
  text-align: center;
  vertical-align: middle;
  border-bottom: 0;
  font-size: 14px;
  font-family: Trebuchet MS;
  font-weight: bold;
}
.admin {
  background-color: #fed6c2;
  border: 1px solid #fed6c2;
  width: 60px;
}
.logout {
  background-color: #2bcb06;
  border: 1px solid #2bcb06;
  width: 80px;
}
.logout a {
  color: #fff;
  font-size: 14px;
  font-family: Lucida Sans Unicode;
  font-weight: bold;
  text-decoration: none;
}
.nav {
  height: 25px;
}
.nav a {
  color: #000; 
  font-weight: bold; 
  font-size: 14px;
}
.arrow {
  width: 20px;
  text-align: center;
  color: #aaa;
}
.image_button {
  background-color: #fff;
  border-bottom: 2px solid #666666;
  border-right: 2px solid #666666;
  border-top: 1px solid #bbbbbb;
  border-left: 1px solid #bbbbbb;
  font-weight: bold;
  cursor: pointer;
  display: inline-block;
  width: 60px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  vertical-align: middle;
  color: #000;
}
<?php echo $page->css; ?>
</style>
</head>
<body>
<div class="superscript">
  <table style="width: 100%;" cellPadding=0 cellSpacing=0>
    <tr>
      <td style="width: 130px;"><img src="exts/uis/<?php echo $path; ?>/assets/imgs/logo.png"></img></td>
      <td style="padding-left: 20px; vertical-align: bottom;">
        <?php 
          if (isset($_SESSION[$sys_script . USER])) {
            include dirname(__FILE__) . '/index_menu.php';
          }
        ?>
      </td>
      <td style="text-align: right; vertical-align: bottom;">
        <?php if (isset($_SESSION[$sys_script . USER])) { ?>
        <table style="margin-left: auto;" cellpadding="0" cellspacing="0">
          <tr>
            <td class="right admin"><a href='?i=23' class="right"><?php echo $_SESSION[$sys_script . USER]->account; ?></a></td>
            <td class="right logout" onclick="location.href='?logout=1'"><a href="?logout=1">Logout</a></td>
          </tr>
        </table>
        <?php } ?>
      </td>
    </tr>
  </table>
</div>
<div>
  <?php include dirname(__FILE__) . '/index_nav.php'; ?>
</div>
<div>
  <table  style="width: 100%;" cellpadding="0" cellspacing="0">
    <tr>
      <td style="padding-left: 20px; text-align: left;vertical-align: top;height: 500px;">
        <?php echo $page->html; ?>
        <script type="text/javascript">
        <?php echo $page->js; ?>
        </script>
      </td>
    </tr>
  </table>
</div>
</body>
</html>
<?php 

function _createMenuRoot($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->link = '?i=' . $tmp->id;
      $tmp->c = array();
      $node->c[] = $tmp;
      _createMenuRoot($arr, $tmp);
    }
  }
}

function _getNav($root, $id) {
  $array = array();
  return _find($root, $id, $array);
}

function _find($haystack, $needle, $array) {
  if ($haystack->id == $needle) {
    return $array;
  }
  foreach ($haystack->c as &$tmp) {
    array_push($array, $tmp);
    $result = _find($tmp, $needle, $array);
    if ($result != NULL) {
      return $result;
    }
    array_pop($array);
  }
  return array();
}

?>
